﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;
using System.Data.Linq;

namespace CP.DBService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class DataService<TEntity>
    {
        protected DBLinq<TEntity> DBLinq
        {
            get
            {
                return DBLinq<TEntity>.CreateInstance();
            }
        }
        protected EntityAdapter<TEntity> EntityAdapter
        {
            get
            {
                return EntityAdapter<TEntity>.CreateInstance();
            }
        }
        protected DBHelper Command
        {
            get
            {
                return DBHelper.CreateInstance();
            }
        }

        public virtual IList<TEntity> GetAll()
        {
            return DBLinq.GetEntities();
        }

        public virtual TEntity AddNew()
        {
            return Activator.CreateInstance<TEntity>();
        }

        public virtual IList<TEntity> GetByParameters(string parameters)
        {
            return DBLinq.GetEntitiesByParameters(parameters);
        }

        public virtual TEntity GetByID(object id)
        {
            return DBLinq.GetEntityByID(id);
        }

        public virtual IList<TEntity> GetByIDs(object[] ids)
        {
            IList<TEntity> entities = new List<TEntity>();
            foreach (object id in ids)
            {
                TEntity entity = GetByID(id);
                if (entity != null)
                    entities.Add(entity);
            }
            return entities;
        }

        public virtual void Insert(TEntity entity)
        {
            DBLinq.InsertEntity(entity);
        }

        public virtual void Update(TEntity entity)
        {
            DBLinq.UpdateEntity(entity);
        }

        public virtual void Delete(TEntity entity)
        {
            DBLinq.DeleteEntity(entity);
        }

        public virtual void DeleteByID(object id)
        {
            DBLinq.DeleteEntityByID(id);
        }

        public virtual void InsertList(TEntity[] entities)
        {
            DBLinq.InsertEntities(entities);
        }

        public virtual void UpdateList(TEntity[] entities)
        {
            DBLinq.UpdateEntities(entities);
        }

        public virtual void DeleteList(TEntity[] entities)
        {
            DBLinq.DeleteEntities(entities);
        }
    }






}
